package com.tanger.task;

import java.util.List;

import com.tanger.common.result.ActionResult;
import com.tanger.common.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 定时任务控制层
 * @Author Hchenbin
 * @Date 2023-12-22 11:13
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/tanger/bts/quartz")
@Api(value = "定时任务",tags="定时任务")
public class QuartzController {

    @Autowired
    private QuartzHandler handler;

    @ApiOperation(value = "获取所有任务")
    @GetMapping(value = "/findAll")
    public ActionResult<List<JobDetails>> findAll() {
        return ActionResult.buildResultSuccess(handler.findAll());
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobClassName", value = "任务类名", required = true, paramType ="query", dataType = "String"),
            @ApiImplicitParam(name = "jobGroupName", value = "任务分组名", required = true, paramType ="query", dataType = "String"),
            @ApiImplicitParam(name = "cronExpression", value = "任务表达式", required = true, paramType ="query", dataType = "String")
    })
    @ApiOperation(value = "保存任务")
    @PostMapping(value = "/save")
    public ActionResult save(@RequestParam String jobClassName,
                             @RequestParam String jobGroupName,
                             @RequestParam String cronExpression) {
        try {
            if (StringUtil.isNull(jobClassName)
                    || StringUtil.isNull(jobGroupName) || StringUtil.isNull(cronExpression)) {
                return ActionResult.buildResultError("参数缺失");
            }
            return handler.addOrUpdateJob(getClass(jobClassName), jobClassName, jobGroupName, cronExpression);
        } catch (Exception e) {
            return ActionResult.buildResultError(e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobClassName", value = "任务类名", required = true, paramType ="query", dataType = "String"),
            @ApiImplicitParam(name = "jobGroupName", value = "任务分组名", required = true, paramType ="query", dataType = "String")
    })
    @ApiOperation(value = "暂停任务")
    @PostMapping(value = "/pause")
    public ActionResult pause(@RequestParam String jobClassName, @RequestParam String jobGroupName) {
        try {
            if (StringUtil.isNull(jobClassName)|| StringUtil.isNull(jobGroupName)) {
                return ActionResult.buildResultError("参数缺失");
            }
            return handler.pauseJob(jobClassName, jobGroupName);
        } catch (Exception e) {
            return ActionResult.buildResultError(e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobClassName", value = "任务类名", required = true, paramType ="query", dataType = "String"),
            @ApiImplicitParam(name = "jobGroupName", value = "任务分组名", required = true, paramType ="query", dataType = "String")
    })
    @ApiOperation(value = "恢复任务")
    @PostMapping(value = "/resume")
    public ActionResult resume(@RequestParam String jobClassName, @RequestParam String jobGroupName) {
        try {
            if (StringUtil.isNull(jobClassName)|| StringUtil.isNull(jobGroupName)) {
                return ActionResult.buildResultError("参数缺失");
            }
            return handler.resumeJob(jobClassName, jobGroupName);
        } catch (Exception e) {
            return ActionResult.buildResultError(e.getMessage());
        }
    }

    @ApiOperation(value = "开始所有任务")
    @PostMapping(value = "/start:all")
    public ActionResult startAll() {
        try {
            return handler.runAJobAll();
        } catch (Exception e) {
            return ActionResult.buildResultError(e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobClassName", value = "任务类名", required = true, paramType ="query", dataType = "String"),
            @ApiImplicitParam(name = "jobGroupName", value = "任务分组名", required = true, paramType ="query", dataType = "String")
    })
    @ApiOperation(value = "开始指定任务")
    @PostMapping(value = "/start")
    public ActionResult start(@RequestParam String jobClassName, @RequestParam String jobGroupName) {
        try {
            if (StringUtil.isNull(jobClassName)|| StringUtil.isNull(jobGroupName)) {
                return ActionResult.buildResultError("参数缺失");
            }
            return handler.runAJobNow(jobClassName, jobGroupName);
        } catch (Exception e) {
            return ActionResult.buildResultError(e.getMessage());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobClassName", value = "任务类名", required = true, paramType ="query", dataType = "String"),
            @ApiImplicitParam(name = "jobGroupName", value = "任务分组名", required = true, paramType ="query", dataType = "String")
    })
    @ApiOperation(value = "删除任务")
    @DeleteMapping(value = "/delete")
    public ActionResult delete(@RequestParam String jobClassName,
                          @RequestParam String jobGroupName) {
        try {
            if (StringUtil.isNull(jobClassName)|| StringUtil.isNull(jobGroupName)) {
                return ActionResult.buildResultError("参数缺失");
            }
            return handler.deleteJob(jobClassName, jobGroupName);
        } catch (Exception e) {
            return ActionResult.buildResultError(e.getMessage());
        }
    }

    @SuppressWarnings("unchecked")
    private static Class<? extends QuartzJobBean> getClass(String classname) throws Exception {
        Class<?> class1 = Class.forName(classname);
        return (Class<? extends QuartzJobBean>) class1;
    }
}
